package com.busionline.wotu.baselibrary.navigation.top;

import android.app.Activity;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * 创建人：chenwuchao
 * 创建时间：2017/5/17 18:57
 */
public abstract class AbsNavigationBar<P extends AbsNavigationBar.Builder.NavigationParams> implements INavigation{
    private  P params;
    private View navigationView;

    public AbsNavigationBar(P params) {
        this.params = params;
        createAndBindView();
    }

    protected String getString(int id) {
        return this.params.context.getResources().getString(id);
    }

    protected int getColor(int id) {
        return ContextCompat.getColor(this.params.context, id);
    }

    protected P getParams() {
        return params;
    }


    /**
     * 创建和绑定视图
     */
    private void createAndBindView() {
        if(params.parent == null){
            //获取activity的根布局  View的源码得到
            ViewGroup activityRoot = (ViewGroup) ((Activity) (params.context)).getWindow().getDecorView();
            params.parent = (ViewGroup) activityRoot.getChildAt(0);
        }

        if(params == null){
            return;
        }
        //1.创建View
         navigationView = LayoutInflater.from(params.context).inflate(bindLayoutId(), params.parent, false);
        //2.加入布局
        params.parent.addView(navigationView,0);
        //3.绑定view
        applyView();
    }

    /**
     * 通过资源id找到布局
     * @param viewId
     */
    protected <T extends View>T findViewById(int viewId){
        return (T) navigationView.findViewById(viewId);
    }

    /**
     * 设置文本
     * @param viewId
     * @param text
     */
    protected void setText(int viewId, CharSequence text) {
        TextView tv = findViewById(viewId);
        if (tv != null) {
            tv.setText(text);
        }
    }

    /**
     * 设置点击事件
     * @param viewId
     * @param listener
     */
    protected void setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = findViewById(viewId);
        if (view != null) {
            view.setOnClickListener(listener);
        }
    }


    /**
     * 设置背景资源
     * @param viewId
     * @param resourceId
     */
    protected void setImageResource(int viewId, int resourceId) {
        ImageView imageView = findViewById(viewId);
        if (imageView != null) {
            imageView.setImageResource(resourceId);
        }
    }


    public abstract static  class  Builder{
        public NavigationParams P;
        public  abstract <T extends AbsNavigationBar>T build();
        public Builder(Context context, ViewGroup parent) {
            P = new NavigationParams(context,parent);
        }

        public Builder(Context context) {
            P = new NavigationParams(context,null);
        }

        // 默认的配置参数
        public static class NavigationParams {
            public Context context;
            public ViewGroup parent;
            public NavigationParams(Context context, ViewGroup parent) {
                this.context = context;
                this.parent = parent;
            }
        }
    }

}
